/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2017-01-05 22:53:57
 * @Description: 
 */

var util = {};

util.setCookie = function(name, value, options) {

    if (options.expires) {
        var expires = new Date();
        expires.setTime(expires.getTime() + (options.expires * 24 * 60 * 60 * 1000));
    }

    //encodeURI方法的作用是进行编码，主要防治value中有特殊字符  
    var str = name + '=' + encodeURI(value) + (expires ? '; expires=' + expires.toGMTString() : '') + (options.path ? '; path=' + options.path : '/') + (options.domain ? '; domain=' + options.domain : '');
    //+ (options.secure ? '; secure' : '');
    //+ (options.httpOnly ? '; HttpOnly' : '');
    //console.log(str);
    document.cookie = str;
};

util.getCookie = function(name) {
    var search = name + "=",
        offset, end;
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) {
                end = document.cookie.length;
            };
            return decodeURI(document.cookie.substring(offset, end));
        }
    }
    return "";
};

util.delCookie = function(name, obj) {
    util.setCookie(name, "", obj);
};

util.rootDomain = function() {
    var arr = window.location.origin.split('.'),
        length = arr.length,
        rootDomain = arr[length - 2] + '.' + arr[length - 1];
    return rootDomain;
};

export default util;
